﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Forms;
namespace OtherDevice
{
    [Description("温度传感器-SM8701")]
    public class Temperature_SM8701:Temperature_Base
    {
        public Temperature_SM8701(string strName) : base(strName)
        {
            controlFrom = new Form_测温();
        }
        public delegate void EventGetTemperatureDelegate(double d_Value);
        public event EventGetTemperatureDelegate EventGetTemperature = null;
        private double d_ActTemperature = 0;
        bool b_Reflush = false;
        public override void InitDeal()
        {
            m_SerialPort.DataReceived += M_SerialPort_DataReceived;
        }
        /// <summary>
        /// 数据接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] byte_Result = null;
                ReceiveData(ref byte_Result, 6000);
                string strReceived = ArrayByteToHexStr(byte_Result);
                //将收到的十六进制字符串 strReceived 以空格拆分成数组
                string[] arrayStrReceived = strReceived.Split(' ');
                string value = arrayStrReceived[3].ToString() + arrayStrReceived[4].ToString();
                double Value = Convert.ToInt32(value, 16);
                d_ActTemperature = Value / 100;
                b_Reflush = true;
                if (EventGetTemperature != null)
                    EventGetTemperature(d_ActTemperature);
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取温度失败"+ex.Message);
            }
        }

        //发送锁
        object objSendSensorLock = new object();
        /// <summary>
        /// 查询温度
        /// </summary>
        /// <returns></returns>
        public override bool Function_查询温度(ref double d_Temperature,int n_TimeOut=3000)
        {
            lock (objSendSensorLock)
            {
                try
                {
                    byte[] bt= new byte[8];
                    bt[0] = 250;
                    bt[1] = 3;
                    bt[2] = 0;
                    bt[3] = 0;
                    bt[4] = 0;
                    bt[5] = 1;
                    bt[6] = 145;
                    bt[7] = 129;
                    b_Reflush = false;
                    int n_RetryTime = 0;
                Retry:
                    SendData(bt);
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Restart();
                    while (true)
                    {
                        if (stopWatch.ElapsedMilliseconds > 500)
                        {
                            n_RetryTime++;
                            if (n_RetryTime > 3)
                                return false;
                            else
                                goto Retry;
                        }
                        if (b_Reflush)
                            break;
                        Thread.Sleep(5);
                    }
                    d_Temperature = d_ActTemperature;
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发送获取温度指令失败" + ex.Message);
                    return false;
                }
            }

        }

    }
}